package com.ep.stepview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ep.stepview.R;

/**
 * Created by Administrator on 2017/7/11.
 */

public class ColorTrackTextView extends TextView {

    private int mOriginColor;
    private int mChangeColor;

    private Paint mOriginPaint;
    private Paint mChangePaint;

    private Direction mdirection= Direction.LEFT_TO_RIGHT;
    private float mCurProgress;

    public ColorTrackTextView(Context context) {
        this(context,null);
    }

    public ColorTrackTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ColorTrackTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView);
        mOriginColor=typedArray.getColor(R.styleable.ColorTrackTextView_originColor,getTextColors().getDefaultColor());
        mChangeColor=typedArray.getColor(R.styleable.ColorTrackTextView_changeColor,getTextColors().getDefaultColor());
        typedArray.recycle();

        mOriginPaint=getColorPaint(mOriginColor);
        mChangePaint=getColorPaint(mChangeColor);

    }

    private Paint getColorPaint(int color) {
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setTextSize(getTextSize());
        paint.setDither(true);
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
   //     super.onDraw(canvas);
        int curOffset= (int) (mCurProgress*getWidth());
        if (mdirection== Direction.LEFT_TO_RIGHT){
            drawText(canvas,mChangePaint,0,curOffset);
            drawText(canvas,mOriginPaint,curOffset,getWidth());
        }
        if (mdirection== Direction.RIGHT_TO_LEFT){
            drawText(canvas,mChangePaint,getWidth()-curOffset,getWidth());
            drawText(canvas,mOriginPaint,0,getWidth()-curOffset);
        }
    }

    public enum Direction{
        LEFT_TO_RIGHT,RIGHT_TO_LEFT
    }
    private void drawText(Canvas canvas,Paint paint,int start,int end){
        canvas.save();
        Rect rect=new Rect(start,0,end,getHeight());
        canvas.clipRect(rect);

        String text=getText().toString();
        Rect textBounds=new Rect();
        paint.getTextBounds(text,0,text.length(),textBounds);
        int dx=getWidth()/2-textBounds.width()/2;

        Paint.FontMetricsInt fontMetricsInt=paint.getFontMetricsInt();
        int dy=(fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom;
        int baseLine=getHeight()/2+dy;
        canvas.drawText(text,dx,baseLine,paint);
        canvas.restore();
    }

    public void setCurProgress(float progress){
        this.mCurProgress=progress;
        invalidate();
    }
    public void setDirection(Direction direction){
        this.mdirection=direction;
    }
}
